/*
 * test_concurrentmap.c
 *
 *  Created on: Feb 17, 2011
 *      Author: thienlong
 */

#include <stdio.h>
#include <pthread.h>
#include "../src/map.h"
#include <unistd.h>

concurrentmap *cmap = NULL;
void *pthread_hanlder(void* arg) {
	unsigned int temp = 0;
//	int counter = 0;
	while (true) {
		int *k = malloc(sizeof(int));
		int *v = malloc(sizeof(int));
		*k = rand_r(&temp) % 1024;
		*v = rand_r(&temp);
		if (arg) {
			cmap_put(cmap, k, v);
			printf("thread %ld: put key/val = %d/%d\n", pthread_self(), *k, *v);
			fflush(stdout);
		} else {
			v = cmap_remove(cmap, k, &k);
			if (v) {
				printf("thread %ld: remove key/val = %d/%d\n", pthread_self(), *k,
						*v);
			}
			fflush(stdout);
		}
	}
}

int main(int argc, char **argv) {
	map_fn fns = {str_hash_fn, str_eq_fn, mem_free, mem_free};
	cmap = cmap_new(&fns, 64, 16);
	pthread_t id = 0;
	pthread_create(&id, NULL, pthread_hanlder, (void *) true);
	pthread_create(&id, NULL, pthread_hanlder, (void *) false);
	while (true) {
		sleep(1000);
	}
}
